package com.nip.workmanager.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nip.workmanagement.R;
import com.nip.workmanager.activity.TodoListActivity;
import com.nip.workmanager.common.ConstantDatabase;
import com.nip.workmanager.common.ConstantIntentCode;
import com.nip.workmanager.entity.ListViewCheckListEntity;

/**
 * 
 * @author ThangNM
 * 
 */
public class CheckListAdapter extends BaseAdapter{

	private Activity _mActivity;
	private List<ListViewCheckListEntity> _mListSource;
	private static LayoutInflater _mInflater;
	private int _index;

	public CheckListAdapter(Activity _mActivity,
			List<ListViewCheckListEntity> _mListSource) {
		super();
		this._mActivity = _mActivity;
		this._mListSource = _mListSource;
		_mInflater = (LayoutInflater) this._mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (this._mListSource.size() <= 0) {
			return 1;
		}
		return this._mListSource.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		View row = arg1;
		ViewHolder viewHolder;

		if (arg1 == null) {
			viewHolder = new ViewHolder();
			row = _mInflater.inflate(R.layout.listview_checklist_layout, null);

			viewHolder.tvWorkName = (TextView) row
					.findViewById(R.id.tvCWorkName);
			viewHolder.tvNotification = (TextView) row
					.findViewById(R.id.tvNotification);
			viewHolder.btnAddTodo = (Button) row.findViewById(R.id.btnTodoList);
			viewHolder.imgIcon = (ImageView) row.findViewById(R.id.imgIconWork);
			viewHolder.btnAddTodo.setTag(arg0);
			row.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) row.getTag();
		}

		if (this._mListSource.size() <= 0) {
			viewHolder.tvWorkName.setText("NO CHECK WORK");
			viewHolder.tvNotification.setText("NO TODO");
		}else
		{
			viewHolder.tvWorkName.setText(this._mListSource.get(arg0).getWorkName());
			viewHolder.tvNotification.setText(this._mListSource.get(arg0).getNotification());
			viewHolder.imgIcon.setImageResource(this._mListSource.get(arg0).getIcon());
			_index = arg0;
			viewHolder.btnAddTodo.setTag(arg0);
			viewHolder.btnAddTodo.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int index = (Integer) v.getTag();
					Intent i = new Intent(_mActivity, TodoListActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString(ConstantIntentCode.SHOW_LIST_TODO, _mListSource.get(index).getWorkId());
					i.putExtras(bundle);
					_mActivity.startActivity(i);
				}
			});
		}
		return row;
	}

	public static class ViewHolder {
		public TextView tvWorkName;
		public TextView tvNotification;
		public ImageView imgIcon;
		public Button btnAddTodo;
	}

}
